package day6;


import org.junit.Test;

import javax.management.monitor.StringMonitor;
import java.util.Arrays;

import static com.sun.org.apache.xerces.internal.util.XMLChar.trim;
import static jdk.javadoc.internal.doclets.toolkit.util.Utils.toLowerCase;

public class StringDemo {
    @Test
    public void test1() {
        // "abc"字符串常量，也是一个对象
        String str1 = "abc";
        String str2 = new String("abc");
        String str3 = "abc";
        //==比较的是地址值
        System.out.println(str1 == str2);// false
        System.out.println(str1 == str3);// true
        System.out.println(str2 == str3);// false
        //equals()比较字符串的内容是不是相等
        System.out.println(str1.equals(str2));//true
        System.out.println(str1.equals(str3));//true
        System.out.println(str2.equals(str3));//true
    }

    @Test
    public void test2() {
        String str = "java AndroidA";
        System.out.println(str.length());//13
        char[] chars = str.toCharArray();
        char ch = str.charAt(5);//A
        System.out.println(ch);
        System.out.println(str.indexOf('A'));//5
        System.out.println(str.indexOf('X'));//-1
        System.out.println(str.indexOf('A', 6));//12
        System.out.println(str.indexOf("And"));//5
        System.out.println(str.lastIndexOf("A"));//12
    }

    @Test
    public void test3() {
        String str1 = "java";
        String str2 = "Java";
        System.out.println(str1.equals(str2));//false
        System.out.println(str1.equalsIgnoreCase(str2));//true

        String str = "java AndroidA";
        System.out.println(str.contains("And"));//true
        System.out.println(str.startsWith("java"));//true
        System.out.println(str.startsWith("java A"));//true
        //offset: 偏移
        System.out.println(str.startsWith("And", 5));//true

        System.out.println(str.endsWith("idA"));//true
        System.out.println(str.isEmpty());//false
        // "" 空字符串，也是一个对象
        System.out.println("".isEmpty());//true
        char ch = ' ';
        System.out.println(str1.equals(str2));
        System.out.println(str1.equalsIgnoreCase(str2));
        System.out.println(str1.equalsIgnoreCase(str2));
    }
    @Test
    public void test26() {
        String str = "java android python";
        String[] array = str.split(" ");
        System.out.println(Arrays.toString(array));
    }



    @Test
    public void test27() {
        String str1 = "java AndroidA";
        String str2 = "Java";
        String str3 = "PHP";
        String str4  = "python";
        String str = str1 + str2 + str3 + str4;
        System.out.println(str);

        StringBuffer buffer = new StringBuffer();
        buffer.append(str1).append(str2).append(str3).append(str4);
        System.out.println(buffer);

    }





//    @Test
//    public void test34() {
//        //String str = "---java Android---";
//        //String str = "------";
//        //String str = "";
//        //String str = "---java Android";
//        //String str = "java Android---";
//        String str = "    java Android    ";
//        String newStr = trim(str);
//        System.out.println(newStr);
//    }
//
//    private String trim(String str) {
//        //"" null 卫操作
//        if (str == null || str.equals("")) {
//            return " ";
//        }
//
//        int startIndex = 0;
//        int endIndex = str.length() - 1;
//
//        while ((startIndex <= endIndex) && (str.charAt(startIndex) == ' ')) {
//            startIndex++;
//        }
//        while ((startIndex <= endIndex) && (str.charAt(endIndex) == ' ')) {
//            endIndex--;
//        }
//
//        return str.substring(startIndex, endIndex + 1);
//    }



    @Test
    public void test45() {
        String str = "Hello Java";
        System.out.println(str.toLowerCase());
        System.out.println(str.toUpperCase());
        String newStr = toLowerCase(str);
        System.out.println(newStr);
    }

//    private String toLowerCase(String str) {
//        char[] array = str.toCharArray();
//        for (int i = 0; i < array.length; i++) {
//            if (array[i] >= 'A' && array[i] <= 'Z') {
//                array[i] = (char)(array[i] + 32);
//            }
//        }
//        return new String(array);
//    }



    @Test
    public void test48() {
        String str = "Hello Java";
        System.out.println(str.toLowerCase());
        System.out.println(str.toUpperCase());
        String newStr = toLowerCase(str);
        System.out.println(newStr);

    }
        private String toLowerCase(String str){
            char[] array = str.toCharArray();
            for (int i = 0; i < array.length; i++) {
                if(array[i] >='A' && array[i] <= 'Z')
                    array[i] = 32;


            }

            return new String(array);
        }

        @Test
    public void test49() {

        String str = "Hello Java";
        String newStr = trim(str);
        System.out.println(newStr);

        }
        private String trim2(String str) {

            if(str == null || str.equals("")) {
                return "";
            }
            int start = 0;
            int end = str.length() - 1;
           while(start <= end && str.charAt(start) == ' ') {
               start++;
           }
            while(end >= start && str.charAt(end) == ' ') {
                end--;
            }
            return str.substring(start, end + 1);
        }


}
